package sy.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import sy.db.mysql.model.KsKj;
import sy.db.mysql.model.KsKstm;
import sy.db.mysql.model.KsTm;
import sy.pageModel.DataGrid;
import sy.pageModel.Json;
import sy.pageModel.SessionInfo;
import sy.services.KsKjServiceI;
import sy.services.KsKstmServiceI;
import sy.services.KsTmServiceI;
import sy.util.ConfigUtil;

/**
 *  @author zxl
 */
@Controller
@RequestMapping("/ksKjController")
public class KsKjController extends BaseController {
	@Autowired
	public KsKjServiceI ksKjService;
	
	@Autowired
	public KsTmServiceI ksTmService;
	
	
	@Autowired
	public KsKstmServiceI ksKstmService;
	
	@RequestMapping("/dataGrid")
	@ResponseBody
	public DataGrid dataGrid(@RequestParam  Map param) {
		return ksKjService.findbyByGrid(param);
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Json add(KsKj t , HttpSession session) {
		SessionInfo sessionInfo = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		Json j = new Json();
		try {
			t.setCJRSTR(sessionInfo.getName());
			t.setCREATEDATE(new Date());
			ksKjService.insert(t);
			j.setSuccess(true);
			j.setMsg("��ӳɹ���");
			j.setObj(t);
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Json edit(@RequestParam Map<String , String> t , HttpSession session) {
		SessionInfo sessionInfo = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		int df = 0;
		String kj = t.get("KJIDSTR");
		for (Map.Entry<String , String>  entry:t.entrySet()) {  
		    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());  
		    KsTm ksTm = this.ksTmService.selectByPrimaryKey(entry.getKey());
		    if(!entry.getKey().equals("KJIDSTR")){
		    	//更新考题答案
		    	KsKstm kskstm = new KsKstm();
		    	kskstm.setID(entry.getKey());
		    	kskstm.setLRDA(entry.getValue());
		    	kskstm.setKJIDSTR(kj);
		    	ksKstmService.updateByPrimaryKeySelective(kskstm);
		    	if( ksTm.getDASTR().equals(entry.getValue())){
				    	df+=10;
				    }
		    	
		    }
		   
		    
		}  
		KsKj  kskj =  new KsKj();
		kskj.setID( kj);
		kskj.setXGRSTR(sessionInfo.getName());
		kskj.setMODIFYDATE(new Date());
		kskj.setKSFSNUM(df);
		this.ksKjService.updateByPrimaryKeySelective( kskj);
		return null;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Json delete(String id) {
		Json j = new Json();
		try {
			ksKjService.deleteByPrimaryKey(id);
			j.setSuccess(true);
			j.setMsg("��ӳɹ���");
		} catch (Exception e) {
			 e.printStackTrace();
			j.setMsg(e.getMessage());
		}
		return j;
	}

	@RequestMapping("/selectByid")
	@ResponseBody
	public KsKj selectByid(String id) {
		return ksKjService.selectByPrimaryKey(id);
	}
	
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/manager")
	public String manager(HttpSession session,HttpServletRequest request) {
		SessionInfo sessionInfo = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		Map map  = new HashMap();
		map.put("CJRSTR", sessionInfo.getName());
		String uuid = UUID.randomUUID().toString();
		KsKj  kskj =  new KsKj();
		kskj.setID( uuid);
		kskj.setCJRSTR(sessionInfo.getName());
		kskj.setCREATEDATE(new Date());
		kskj.setKSFSNUM(0);
		this.ksKjService.insert(kskj);
		
		map.put("ID",uuid);
		this.ksTmService.findKSTM(map  );
		 map = new HashMap();
		 map.put("KJIDSTR_EQ", uuid);
		DataGrid grid = ksKstmService.findbyByGrid(map);
		request.setAttribute("list", grid.getRows());
		return "/ksKj/ksKj";
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/managerlist")
	public String managerlist() {
		return "/ksKj/ksKjlist";
	}
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/addPage")
	public String addPage(HttpServletRequest request) {
		KsKj ksKj  = new KsKj();
		ksKj.setID(UUID.randomUUID().toString());
		request.setAttribute("ksKj", ksKj);
	return "/ksKj/ksKjAdd";
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/editPage")
	public String editPage(String id ,HttpServletRequest request) {
		KsKj ksKj = this.ksKjService.selectByPrimaryKey(id);
		request.setAttribute("ksKj", ksKj);
		return "/ksKj/ksKjEdit";
	}
	
	/**
	 * 
	 * @return
	 */
	@RequestMapping("/viewPage")
	public String viewPage(String id ,HttpServletRequest request) {
		KsKj ksKj = this.ksKjService.selectByPrimaryKey(id);
		request.setAttribute("ksKj", ksKj);
		return "/ksKj/ksKjView";
	}
}
